package com.yystar.support.base.domain;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

import com.yystar.support.util.WMathUtility;

public class Stake extends BaseDomain {
	
	public double amount;
	
	public Stake(){
		
	}
	
	public Stake(double amount) {
		this.amount = amount;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}
	public String getShowAmount() {
		return getNumberFormatter(2, true).format(amount);
	}
	public String getShowEditAmount() {
		return getNumberFormatter(0, false).format(amount);
	}
	public String getShow0Amount() {
		return getNumberFormatter(0, true).format(Math.floor(amount));
	}
	
	public String getShow0MinAmount() {
		return getNumberFormatter(0, true).format(Math.ceil(amount));
	}

	public String getShow2Amount() {
		return getNumberFormatter(2, true).format(roundDown(amount, 2));
	}
	
	public String getShow0RoundAmount() {
		return getNumberFormatter(0, true).format(WMathUtility.round(amount, 0));
	}

	
	public static NumberFormat getNumberFormatter(int fractionDigits, boolean useGrouping) {
		NumberFormat nf = null;

		nf = NumberFormat.getInstance();

		nf.setMinimumFractionDigits(fractionDigits);
		nf.setMaximumFractionDigits(fractionDigits);
		nf.setGroupingUsed(useGrouping);
		
		if (fractionDigits == 2 && useGrouping) {
			nf.setRoundingMode(RoundingMode.DOWN);
		}

		return nf;
	}
	
	public static double roundDown(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		v = (v >= 0 ? v + 0.0000001 : v - 0.0000001);
		return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(1), scale, BigDecimal.ROUND_DOWN).doubleValue();
	}
}
